package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.course.CourseQueryRequest;
import com.gr.smartedubackend.model.entity.Course;
import com.gr.smartedubackend.service.CourseService;
import com.gr.smartedubackend.mapper.CourseMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
* @author 尘轩
* @description 针对表【course(课程)】的数据库操作Service实现
* @createDate 2025-07-02 13:05:24
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Override
    public QueryWrapper<Course> getQueryWrapper(CourseQueryRequest courseQueryRequest) {

        ThrowUtils.throwIf(courseQueryRequest == null, ErrorCode.PARAMS_ERROR);

        String courseName = courseQueryRequest.getCourseName();
        String courseType = courseQueryRequest.getCourseType();
        String targetGrade = courseQueryRequest.getTargetGrade();
        String description = courseQueryRequest.getDescription();
        String sortField = courseQueryRequest.getSortField();
        String sortOrder = courseQueryRequest.getSortOrder();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(courseName), "courseName", courseName);
        queryWrapper.like(StrUtil.isNotEmpty(courseType), "courseType", courseType);
        queryWrapper.like(StrUtil.isNotEmpty(targetGrade), "targetGrade", targetGrade);
        queryWrapper.like(StrUtil.isNotEmpty(description), "description", description);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




